package com.gcstorage.tvdev.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author: liangkun
 * @date: 2022/4/8
 * @des:
 */
public class CustomWebView extends WebView {
    public CustomWebView(@NonNull Context context) {
        super(context);
    }

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {

            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:

//                showOption();
                break;

            case KeyEvent.KEYCODE_BACK:    //返回键

//                showExitDialog();
                return true;   //这里由于break会退出，所以我们自己要处理掉 不返回上一层

            case KeyEvent.KEYCODE_SETTINGS: //设置键


                break;

            case KeyEvent.KEYCODE_DPAD_DOWN:   //向下键

                /*    实际开发中有时候会触发两次，所以要判断一下按下时触发 ，松开按键时不触发
                 *    exp:KeyEvent.ACTION_UP
                 */

                if (event.getAction() == KeyEvent.ACTION_DOWN) {


                }


                break;

            case KeyEvent.KEYCODE_DPAD_UP:   //向上键


                break;

            case KeyEvent.KEYCODE_0:   //数字键0


                break;

            case KeyEvent.KEYCODE_DPAD_LEFT: //向左键



                break;

            case KeyEvent.KEYCODE_DPAD_RIGHT:  //向右键

                break;

            case KeyEvent.KEYCODE_INFO:    //info键

                break;

            case KeyEvent.KEYCODE_PAGE_DOWN:     //向上翻页键
            case KeyEvent.KEYCODE_MEDIA_NEXT:


                break;


            case KeyEvent.KEYCODE_PAGE_UP:     //向下翻页键
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:


                break;

            case KeyEvent.KEYCODE_VOLUME_UP:   //调大声音键


                break;

            case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键


                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音

                break;

            case KeyEvent.KEYCODE_HOME://房子

                break;
            case KeyEvent.KEYCODE_MENU://菜单

                break;

            default:
                break;
        }

        return super.onKeyUp(keyCode, event);
    }

    /**
     * 监听遥控器按键事件，交由系统处理
     */
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            return false;
        }

        return super.dispatchKeyEvent(event);
    }
}